#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QLabel>


ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(320,588);//设置游戏的固定尺寸
    this->setWindowTitle("请选择关卡~");//设置标题
    this->setWindowIcon(QPixmap(":/resource/Coin0001.png"));//设置图标

    //不用ui，在c++的class中创建菜单栏
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);

    QMenu * startMenu = bar->addMenu("开始"); //创建菜单
    QAction * quitAction = startMenu->addAction("退出");//创建菜单“退出”项；
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });//监听退出

    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){
        //qDebug()<<"RETURN";

        QTimer::singleShot(500,this,[=](){
            emit this->ChooseSceneBack();//延时 发送点击返回的signals
        });
      });

    pScene=NULL;//对游戏场景进行置空


    //创建关卡按钮 循环矩阵
    for(int i=0;i<20;i++){
        MyPushButton * menuBtn = new MyPushButton(":/resource/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+(i%4)*70,130+(i/4)*70);
        connect(menuBtn,&MyPushButton::clicked,[=](){
          // qDebug()<<"您选择的是："<<i+1<<"关";

            this->pScene = new PlayScene(i+1);
            this->pScene->show();
            this->hide();

            //监听第三个场景的返回信号，监听的位置很重要
            connect(this->pScene,&PlayScene::ChooseSceneBack,[=](){
               this->show();//选关场景重新显示
                this->pScene->hide(); //隐藏

                delete this->pScene;
                this->pScene = NULL;//每个关卡不一样
            });


        });
    //显示数字 用Qlabel
        QLabel * label =new QLabel;
        label->setParent(this);
        label->move(25+(i%4)*70,130+(i/4)*70);//自带的Qlabel的大小？？与menubtn大小相同
    //设置Qlabel的尺寸
        label->setFixedSize(menuBtn->width(),menuBtn->height());
    //设置文本
        label->setText(QString :: number(i+1));
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//设置文本对齐
    //设置属性 鼠标可以穿透触发clicked事件
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }



}



void ChooseLevelScene::paintEvent(QPaintEvent *)//和主页面一样设置背景和标题
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/resource/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/resource/Title.png");
    painter.drawPixmap((this->width()-pix.width()) * 0.5,30,pix);
}
